# encoding:utf-8
import json
import io
import MySQLdb

# 把字段存入json文件中
class DataspiderPipeline(object):
    def __init__(self):
        self.file = io.open("data.json", 'a', encoding='utf-8')
        self.conn = MySQLdb.connect(host="127.0.0.1",port=3306,user="root",passwd="123456",db="guns",charset="utf8")
        self.cursor = self.conn.cursor()

    def process_item(self, item, spider):
        # line = json.dumps(dict(item), ensure_ascii=False) + "\n"
        updateId = 'SHKZ08886'
        
        self.cursor.execute("INSERT INTO member(mid,name,size,card,level,parent,settle_persion,period,time,status,update_id) VALUES (%s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s)", 
        	(item['mid'], item['name'], item['size'], item['card'], item['level'], item['parent'], item['settlePersion'], item['period'], item['time'], item['status'], updateId))
        
        self.conn.commit()

	def close_spider(self, spider):
		self.conn.close()
		self.cursor.close()